package com.winit.common.util;

import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.beans.RoleInfo;
import org.slf4j.Logger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;

/**
 * 操作记录
 * Created by wangjian on 16/5/11.
 */
public class LogUtil {

    public enum OperateType {
        BUILD("构建版本"),
        CREATE_NEW_EDITION("生成新版本"),
        CREATE_NEW_EDITION_SUCCESS("生成新版本成功"),
        UPDATE_CONFIG("更新配置文件"),
        SUBMIT_TO_TEST("版本提交测试"),
        SUBMIT_NOT_TO_TEST("取消版本提交测试"),
        EDITION_PROTECT("版本保护"),
        EDITION_NOT_PROTECT("取消版本保护"),
        TEST_PASS("测试通过"),
        TEST_NOT_PASS("测试不通过"),
        CREATE_PUSH_RECORD("生成推送记录"),
        RE_PUSH("重新推送"),
        AUTO_RELEASE("一键发布"),
        MAVEN_TEST("执行maven测试"),
        RELEASE_PREPARE("准备"),
        RELEASE_OFFLINE("下线tomcat"),
        RELEASE_KILL_SERVER("停服务器"),
        RELEASE_BACKUP("备份应用"),
        RELEASE_SCP("拷贝应用"),
        RELEASE_INSTALL("安装"),
        RELEASE_SCP_PATCH("拷贝补丁"),
        RELEASE_START_SERVER("启动服务器"),
        RELEASE_CHECK("检查服务器启动状态"),
        RELEASE_VERIFY("验证"),
        RELEASE_ONLINE("挂载tomcat"),
        RELEASE_ROLLBACK("恢复备份"),
        VM_RELEASE("虚拟机发布"),
        RESTART_SERVER("重启服务器"),
        AUTHORIZE("授权发布"),
        CANCEL_AUTHORIZE("取消授权发布");

        private String desc;

        OperateType(String desc) {
            this.desc = desc;
        }


        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

    public static void operateLog(LoginUser userInfo, String projectName, String editionNum,
                                  String envName, OperateType type) {
        String log = createLogStr(userInfo, projectName, editionNum, envName, type, "", "");
        writeLog(log);
    }

    public static void operateLog(LoginUser userInfo, String projectName, String editionNum,
                                  String envName, OperateType type, String planName, String recordName) {
        String log = createLogStr(userInfo, projectName, editionNum, envName, type, planName, recordName);
        writeLog(log);
    }

    private static String createLogStr(LoginUser userInfo, String projectName, String editionNum,
                                       String envName, OperateType type, String planName, String recordName) {
        StringBuffer log = new StringBuffer();

//        String date = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss SSS");

        log.append(" 用户名:(").append(userInfo.getUserName()).append(") 角色:(|");
        for (RoleInfo roleInfo : userInfo.getUserRole()) {
            log.append(roleInfo.getRole_name()).append("|");
        }
        log.append(") 工程名称:(").append(projectName).append(") 版本号:(").append(editionNum).append(") 环境名称:(")
                .append(envName).append(") 操作:").append(type).append("(").append(type.getDesc()).append(")");
        if (!StringUtils.isEmpty(planName)) {
            log.append(" 计划名称:(").append(planName).append(")");
        }
        if (!StringUtils.isEmpty(recordName)) {
            log.append(" 记录名称:(").append(recordName).append(")");
        }
        return log.toString();
    }

    private static synchronized void writeLog(String log) {
        String[] param = new String[4];
        param[0] = "bash";
        param[1] = Constants.SHELL_PATH + "shell/beetle_write_log.sh";
        param[2] = getLogFilePath();
        param[3] = log;
        ShellRunUtil.runShell(param);
    }

    private static String getLogFilePath() {
        StringBuffer path = new StringBuffer();
        path.append(ConfigUtil.getValue("rootPath")).append("Beetle/logs/log-").append(DateUtil.getCurDateStr("yyyyMMdd")).append(".log");
        return path.toString();
    }

    public static void echoThreadCount(Logger logger, ThreadPoolTaskExecutor taskExecutor, String executorName) {
        logger.info("添加线程,executorName=" + executorName + ",count=" + taskExecutor.getActiveCount());
    }

}
